{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "450d7342",
   "metadata": {},
   "source": [
    "# Numpy (Numerical Python)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f4abc40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 2, 2, 2]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python列表无法进行数组运算\n",
    "# 使用+实现的是列表的拼接\n",
    "# Python列表并不是数组\n",
    "a = [1, 1, 1]\n",
    "b = [2, 2, 2]\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b3dd26bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 3 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# numpy.ndarray对象：N维数组\n",
    "a = np.array([1, 1, 1])\n",
    "b = np.array([2, 2, 2])\n",
    "c = a + b\n",
    "print(c)\n",
    "print(type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd475da5",
   "metadata": {},
   "source": [
    "## ndarray对象的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89f77062",
   "metadata": {},
   "outputs": [],
   "source": [
    "# .ndim 数组维度\n",
    "print(a.ndim)\n",
    "d = np.array([\n",
    "  [1, 2, 3],\n",
    "  [4, 5, 6]\n",
    "]) # 二维数组，2行3列\n",
    "print(d.ndim)\n",
    "\n",
    "# .shape 数组形状，返回行列元组\n",
    "print(d.shape)\n",
    "\n",
    "# .size 元素总数，行和列的乘积\n",
    "print(d.size)\n",
    "\n",
    "# .dtype 元素数据类型\n",
    "print(d.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7373ccb7",
   "metadata": {},
   "source": [
    "## 数组切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "931dde31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [0 0 0]]\n",
      "[[1 2 3]]\n",
      "[[1 2 3]\n",
      " [0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "print(d[:])\n",
    "print(d[:1])\n",
    "\n",
    "# 将所有大于3的元素修改为0\n",
    "d[d>3] = 0\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd1d9bad",
   "metadata": {},
   "source": [
    "## 运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c28d45a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 10 15]\n",
      " [ 0  0  0]]\n",
      "[[1 0]\n",
      " [2 0]\n",
      " [3 0]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵运算\n",
    "print(d * 5)\n",
    "\n",
    "# 转置\n",
    "print(d.transpose())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
